using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using CMS.BIZ;

namespace CMS.Web.UI.UserControls
{
    public class ForumEditor : UserControl
    {

        private BizForumCategory category;
        private BizForum forum;

        protected Repeater repeaterCategory;
        protected Repeater repeaterForum;

        protected TextBox tbName;
        protected TextBox tbDescription;
        protected Literal litCategory;
        protected Label lCategory;
        protected DropDownList ddlCategory;
        protected Button bValidateCategory;
        protected Button bValidateForum;


        public ForumEditor()
        {
            base.Init += new EventHandler(ForumEditor_Init);
        }

        protected void ForumEditor_Init(object sender, EventArgs e)
        {
            if (bValidateCategory != null) bValidateCategory.Click += new EventHandler(ValidateCategory_Click);
            if (bValidateForum != null) bValidateForum.Click += new EventHandler(ValidateForum_Click);
            if (!Page.IsPostBack)
            {
                string delete = Request.QueryString["Delete"];
                string deleteCatg = Request.QueryString["DeleteCategory"];
                if (!string.IsNullOrEmpty(delete)) Page.BizForumProvider.Delete(int.Parse(delete));
                if (!string.IsNullOrEmpty(deleteCatg)) Page.BizForumCategoryProvider.Delete(int.Parse(deleteCatg));
                SetCategoryFromQueryString();
                SetForumFromQueryString();
                BindCategory();
                BindForum();
                if (category != null)
                {
                    tbDescription.Text = category.ForumCategoryDescription;
                    tbName.Text = category.ForumCategoryName;
                    ddlCategory.Visible = false;
                    lCategory.Visible = false;
                    bValidateForum.Visible = false;
                }
                if (forum != null)
                {
                    tbDescription.Text = forum.ForumDescription;
                    tbName.Text = forum.ForumName;
                    if(forum.ForumCategoryID != null) ddlCategory.SelectedValue = forum.ForumCategoryID.ToString();
                    bValidateCategory.Visible = false;
                }
            }
        }

        void ValidateForum_Click(object sender, EventArgs e)
        {
            bool update = true;
            if (forum == null) SetForumFromQueryString();
            if (forum == null)
            {
                update = false;
                forum = new BizForum();
            }
            forum.ForumDescription = tbDescription.Text;
            forum.ForumName = tbName.Text;
            forum.ForumCategoryID = null;
            if (!string.IsNullOrEmpty(ddlCategory.SelectedValue)) forum.ForumCategoryID = int.Parse(ddlCategory.SelectedValue);
            if (update) Page.BizForumProvider.Update(forum);
            else Page.BizForumProvider.Create(forum);
            BindForum();
        }

        private void BindForum()
        {
            List<BizForum> list = Page.BizForumProvider.ReadAll("ForumName");
            repeaterForum.DataSource = list;
            repeaterForum.DataBind();
        }

        private void BindCategory()
        {
            List<BizForumCategory> list = Page.BizForumCategoryProvider.ReadAll("ForumCategoryName");
            ddlCategory.DataSource = list;
            ddlCategory.DataValueField = "ForumCategoryID";
            ddlCategory.DataTextField = "ForumCategoryName";
            ddlCategory.DataBind();
            ddlCategory.Items.Insert(0,new ListItem("", ""));
            repeaterCategory.DataSource = list;
            repeaterCategory.DataBind();
        }

        protected void ValidateCategory_Click(object sender, EventArgs e)
        {
            bool update = true;
            if (category == null) SetCategoryFromQueryString();
            if (category == null)
            {
                update = false;
                category = new BizForumCategory();
            }
            category.ForumCategoryDescription = tbDescription.Text;
            category.ForumCategoryName = tbName.Text;
            if (update) Page.BizForumCategoryProvider.Update(category);
            else Page.BizForumCategoryProvider.Create(category);
            BindCategory();
        }

        private void SetCategoryFromQueryString()
        {
            string str = Request.QueryString["ForumCategory"];
            if (!string.IsNullOrEmpty(str)) category = Page.BizForumCategoryProvider.Find(int.Parse(str));
        }

        private void SetForumFromQueryString()
        {
            string str = Request.QueryString["Forum"];
            if (!string.IsNullOrEmpty(str)) forum = Page.BizForumProvider.Find(int.Parse(str));
        }


    }
}
